#include <stdio.h>
#include "unit.h"
#include "els_vm.h"
#include "func.h"
#include "els_vm_bytecode.h"

ELS_FUNC *func_root= NULL;

void els_vm_insertfunction ( ELS_FUNC *f){
    els_pack();
    f->next = func_root;
    func_root = f;
    f->marked = 0;
}

static void freefunc (ELS_FUNC *f){
    els_vm_free (f->code);
    els_vm_free (f);
}

Long els_vm_funccollector (void){
    ELS_FUNC *curr = func_root;
    ELS_FUNC *prev = NULL;
    Long ctr = 0;
    while (curr){
        ELS_FUNC *next = curr->next;
        if (!curr->marked){
            if (prev == NULL) func_root = next;
            else prev->next = next;
            freefunc (curr);
            ++ctr;
        }else{
            curr-> marked = 0;
            prev = curr;
        } 
        curr = next;
    }
    return ctr;
}



